/**
 * 世界排行
 */
class RankItem extends eui.ItemRenderer{
    private oldData:any;
    private img_rank:eui.Image;
    private lb_rank:eui.BitmapLabel;
    private img_mask:eui.Image;
    private img_head:eui.Image;
    private lb_name:eui.Label;
    private lb_customs:eui.Label;
    constructor()
    {
        super();
        this.skinName = 'RankItemView';
        this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.onRemoveToStageHandler, this);
    }

    private onRemoveToStageHandler(e:egret.Event){
		this.clear();
	}

    public init()
    {
        let data = this.data;
        this.touchEnabled = false;
        if(!this.data)return;

        let avatar = data.avatar_url;
        let score = data.endless_score;
        let customs = data.level;
        let name = data.nick_name;
        let rank = this.itemIndex+1;
        if(rank <= 3)
        {
            this.img_rank.source = 'rank_'+rank+'_png';
            this.lb_rank.visible = false;
            this.img_rank.visible = true;
        }
        else
        {
            this.img_rank.visible = false;
            this.lb_rank.visible = true;
        }
        this.lb_name.text = StringUtil.getNameSlice(name);
        this.img_head.source = avatar;
        this.img_head.width = this.img_head.height = 76;
        this.img_head.mask = this.img_mask;

        if(rank == 0)
        {
            this.lb_rank.text = '99+';
        }
        else
        {
            this.lb_rank.text = rank + '';
        }
        
        if(score != null)
        this.lb_customs.text = score+'分';
        if(customs != null)
        this.lb_customs.text = `第${customs}关`;
    }

    public dataChanged()
    {
        if(this.data == this.oldData)return;
        this.oldData = this.data;
        this.init();
    }

    public clear()
    {
        this.data = null;
        this.oldData = null;
        this.removeEventListener(egret.Event.REMOVED_FROM_STAGE, this.onRemoveToStageHandler,this);
    }
}

window['RankItem'] = RankItem;